package com.easepal802s.project.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.ble.HexUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 该控件用以实现长按触发多次点击事件的效果
 *
 * Created by FHB on 2018/4/9.
 */
public class LongClickImageView extends ImageView {

    /**
     * 长按事件触发频率
     */
    private long mDelayMillis = 505;

    private boolean isMotionEventUp = true;
    private Disposable mDisposable;
    private ScrollView mScroll;
    public LongClickImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        intiListener();
    }
    public void setScollView(ScrollView s){
        mScroll =s;

    }
    public LongClickImageView(Context context) {
        super(context);
        intiListener();
    }

    public LongClickImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public void intiListener() {
        this.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                isMotionEventUp = false;
                if(mDisposable==null){
                    sendReplyOk();
                }
                return false;
            }
        });
        this.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_CANCEL:
                        if(mDisposable!=null){

                            Log.e("LongNextonTouch2","ACTION_CANCEL");
                            mDisposable.dispose();
                            mDisposable = null;
                            BleController.getInst().writeDatas(HexUtils.hexToByte(BleConstant.PONG));

                            // 防止滑动冲突
                            if(mScroll!=null){
                                mScroll .requestDisallowInterceptTouchEvent(false);
                            }
                            BleController.getInst().setIsOnReplyOk();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.e("LongNextonTouch4","ACTION_UP");
                        if(mDisposable!=null){

                            BleController.getInst().writeDatas(HexUtils.hexToByte(BleConstant.PONG));
                            mDisposable.dispose();
                            mDisposable = null;
                            // 防止滑动冲突
                            if(mScroll!=null){
                                mScroll .requestDisallowInterceptTouchEvent(false);
                            }
                            BleController.getInst().setIsOnReplyOk();
                        }
                        break;
                }

                return false;
            }
        });
    }

//    /**
//     * 用以处理click事件
//     */
//    Handler mHandler = new Handler(Looper.getMainLooper()) {
//        public void handleMessage(android.os.Message msg) {
//            Log.e("isMotionEventUp22",isMotionEventUp+"");
//            if (!isMotionEventUp && isEnabled()) {
//                // 调用click事件
//                performClick();
//                mHandler.sendEmptyMessageDelayed(0, mDelayMillis);
//            }
//        };
//    };

    /**
     * 设置触发时间间隔
     *
     * @param delayMillis
     */
    public void setmDelayMillis(long delayMillis) {
        this.mDelayMillis = delayMillis;
    }

    public synchronized void sendReplyOk() {
        Observable.interval(0, mDelayMillis, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {
                        Log.e("LongNext","LongNext");
                       mHandler.sendEmptyMessage(0);

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

        Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(android.os.Message msg) {
                // 调用click事件
                performClick();
        }
    };

}
